package com.youlu.campus.service.channel;

import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.entity.AppUserShareActivity;
import com.youlu.campus.entity.channel.AppChannel;
import com.youlu.campus.entity.channel.dto.AppChannelDTO;
import com.youlu.campus.entity.channel.dto.AppChannelQueryDTO;
import com.youlu.campus.entity.commission.dto.DistributorBuildLinkDTO;
import com.youlu.campus.service.activity.ActivityService;
import com.youlu.campus.service.platform.AppUserShareActivityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;


@Slf4j
@Service
public class AppChannelService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private AppUserShareActivityService appUserShareActivityService;

   public Page<AppChannel> list(AppChannelQueryDTO dto){
       Integer pageNo = dto.getPageNo();
       Integer pageSize = dto.getPageSize();
       Query  query = new Query();
       PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
       long count = this.mongoTemplate.count(query, AppChannel.class);
       if(count==0){
           return PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> count);
       }
//       query.with(new Sort(Sort.Direction.DESC, "createdTime"));
       Sort sort=Sort.by(Sort.Direction.DESC, "createdTime");
       query.with(sort);
       List<AppChannel> channelList = this.mongoTemplate.find(query.with(pageRequest), AppChannel.class);
       channelList.stream().forEach(appChannel -> {
           appChannel.setInnerLink(appUserShareActivityService.getMiniPageUrl(appChannel));
           if(StringUtils.isNotBlank(appChannel.getActivityId())){
               appChannel.setOuterLink(appUserShareActivityService.getChannelLinkByChannelIdAndActivityId(appChannel.getId(),appChannel.getActivityId()));
           }else{
               appChannel.setOuterLink(appUserShareActivityService.getChannelLinkByChannelIdAndActivityId(appChannel.getId(),appChannel.getId()));
           }

       });
       return PageableExecutionUtils.getPage(channelList, pageRequest, () -> count);
    }


    public void delete(String id){
       Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
       this.mongoTemplate.remove(query,AppChannel.class);
        query = new Query();
        query.addCriteria(Criteria.where("userId").is(id));
        query.addCriteria(Criteria.where("activityId").is(id));
        this.mongoTemplate.remove(query, AppUserShareActivity.class);
    }


    public void save(AppChannelDTO dto){
        String channelName = dto.getChannelName();
        String activityId = dto.getActivityId();
        Integer linkJumpType = dto.getLinkJumpType();
        AppChannel appChannel = null;
        Date now = new Date();
        log.info("【应用渠道】channelName:{},linkJumpType:{},activityId:{}",channelName,linkJumpType,activityId);
             appChannel = new  AppChannel ();
             appChannel.setActivityId(activityId);
            appChannel.setChannelName(channelName);
            ActivityInfo activityInfo = activityService.findOne(activityId);
            if(Objects.nonNull(activityInfo)){
                appChannel.setActivityName(activityInfo.getName());
            }
            appChannel.setCreatedTime(now);
            appChannel.setUpdatedTime(now);
            appChannel.setLinkJumpType(linkJumpType);
            AppChannel insertAppChannel = this.mongoTemplate.insert(appChannel);
            DistributorBuildLinkDTO distributorBuildLink = new DistributorBuildLinkDTO();
            distributorBuildLink.setUserId(insertAppChannel.getId());
            if(StringUtils.isNotBlank(activityId)){
                distributorBuildLink.setActivityId(activityId);
            }else{
                distributorBuildLink.setActivityId(insertAppChannel.getId());
            }
            distributorBuildLink.setShareLinkType(4);
            if(linkJumpType==2){
                distributorBuildLink.setPageType(2);
            }
            appUserShareActivityService.buildChannelLink(distributorBuildLink);
    }
}
